//
//  ManTicketListVC.swift
//  ManGuangDB
//
//  Created by 李东 on 2024/3/26.
//

import UIKit
import SnapKit

class ManTicketListVC: ManBaseVC {
    
    lazy var emptyImgView = UIImageView(image: UIImage(named: "img_empty"))
    var talbeView: UITableView!
    let cellID = "celliD"
    lazy var list = [ManTicketModel]()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        title = "珍贵票票"
        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "icon_add")?.withRenderingMode(.alwaysOriginal), style: .done, target: self, action: #selector(addHandle))
        initUI()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        list = ManDataHelper.shared.getLocalList(isRefresh: true)
        talbeView.reloadData()
        emptyImgView.isHidden = !list.isEmpty
    }

    
    @objc func addHandle() {
        navigationController?.pushViewController(ManAddTicketVC(), animated: true)
    }

    func initUI() {
        view.addSubview(emptyImgView)
        emptyImgView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        talbeView = UITableView(frame: .zero, style: .grouped)
        talbeView.backgroundColor = .clear
        talbeView.delegate = self
        talbeView.dataSource = self
        talbeView.rowHeight = 140
        talbeView.separatorStyle = .none
        talbeView.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: CGFloat.screenWidth, height: 0.01))
        talbeView.register(UINib(nibName: "ManTicketCell", bundle: nil), forCellReuseIdentifier: cellID)
        view.addSubview(talbeView)
        talbeView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
  
    

}

extension ManTicketListVC : UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        list.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! ManTicketCell
        cell.model = list[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        navigationController?.pushViewController(ManAddTicketVC(model: list[indexPath.row]), animated: true)
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        nil
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        0.01
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        nil
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        0.01
    }
}
